<?php
Namespace Cyfy\Modules\Forms;

use \Cyfy\Message;

/**
 * Button element.
 *
 * Creating INPUT elements, type submit or reset.
 * It's possible to change their standard behaviour by $onClick variable.
 * Usage:
 * <code>
 * // called in method structure() of a form, that's why there is $this
 * $b = new Forms\Button('Custom Button');
 * $b -> onClick = "alert('Hello world!'); return false";
 * $this -> addButton($b);
 * </code> 
 *
 * @author Dandelion<dandelion8888@gmail.com>
 * @version 0.1
 */
class Button
{
    /**
     * Default button type
     */
    const OK = "OK";
    
    /**
     * Resets form
     */
    const RESET = "reset";
    
    /** 
     * Deletes something - must be combined with onClick variable to work.
     * These types are because of button stylisation.
     */
    const DELETE = "delete";
    
    /**
     * Cancels something - must be combined with onClick variable to work.
     * These types are because of button stylisation.
     */
    const CANCEL = "cancel";
    
    /**
     * Button attribute name. Not neccessary.
     * @var string
     */
    public $name = '';
    
    /**
     * Text shown on the button.
     * @var string
     */
    public $title = 'Button';
    
    /** 
     * Button type - see constants.
     * @var string
     */
    public $type = 'OK';
    
    /**
     * JavaScript onClick callback. JavaScript code in string.
     * @var string
     */
    public $onClick = '';
    
    /**
     * Custom ID of the button
     * @var string
     */
    public $id = '';
    
    /**
     * Creates a new button, type submit by default.
     *
     * @param string Title on the button.
     * @param string Button type, see class constants.
     */
    function __construct($title, $type = "OK")
    {
        $this -> title = $title;
        $this -> type = $type;
    }
     
    /**
     * Returns serialized button.
     * @return string HTML code.
     */
    function __toString()
    {
        // if type == reset, then reset, else submit
        $type = ($this -> type == self :: RESET) ? "reset" : "submit";
        // onclick
        $o = "";
        if($this -> onClick)
        {
            $o = "onclick=\"{$this -> onClick}\"";
        }
        // id
        $id = ($this->id) ? "id='{$this->id}'" : "";
        
        return "<input type=\"{$type}\" {$id} value=\"{$this->title}\" class=\"form-button-{$this->type}\" $o/> \n";;
    }
}